﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataWinner.Nba.DataAccess.Sina;
using DataWinner.Entity.Nba_Sina.Models;
using DataWinner.Nba.Model.Portal;
using System.Data.Entity;


namespace DataWinner.Business.Portal
{
    public class BsAnalysis
    {
        DaTeam daTeam = new DaTeam();
        DaTeamStat daTeamStat = new DaTeamStat();
        DaMatch daMatch = new DaMatch();
        DaTeamStatDaily daTeamStatDaily = new DaTeamStatDaily();


        public AnalysisIndexModel GetAnalysisIndexModel(DateTime date)
        {
            AnalysisIndexModel result = new AnalysisIndexModel();

            DateTime dateBegin = date.Date;
            DateTime dateEnd = date.Date.AddDays(1);

            List<TeamStat_Sina> teamStatSet = daTeamStat.GetTeamStatByMatchDate(dateBegin, dateEnd).ToList();
            List<Match_Sina> matchSet = daMatch.GetMatchListByDate(dateBegin, dateEnd).ToList();


            List<MatchModel> matchModelSet = new List<MatchModel>();
            foreach (Match_Sina match in matchSet)
            {
                MatchModel matchModel = new MatchModel()
                {
                    Match = match,
                    HomeTeamStat = teamStatSet.FirstOrDefault(s => s.TeamId == match.Team_Sina.TeamId && s.MatchId == match.MatchId),
                    VisitingTeamStat = teamStatSet.FirstOrDefault(s => s.TeamId == match.Team_Sina1.TeamId && s.MatchId == match.MatchId)
                };
                matchModelSet.Add(matchModel);
            }
            result.MatchModelSet = matchModelSet;
            return result;
        }

        public AnalysisDetailsModel GetAnalysisDetailsModel(string matchId)
        {
            AnalysisDetailsModel result = new AnalysisDetailsModel();

            Match_Sina mainMatch = daMatch.Set.FirstOrDefault(m => m.MatchId == matchId);

            result.Match = mainMatch;

            int year = result.Match.MatchTime.Year;

            if (result.Match.MatchTime.Month < 9)
            {
                year--;
            }

            List<Team_Sina> teamSet = daTeam.Set.ToList();
            List<Match_Sina> matchSet = daMatch.Set.Where(m => (m.HomeTeamId == mainMatch.HomeTeamId || m.HomeTeamId == mainMatch.VisitingTeamId
                    || m.VisitingTeamId == mainMatch.HomeTeamId || m.VisitingTeamId == mainMatch.VisitingTeamId)
                    && (m.SeasonYear == year) && m.MatchTime < mainMatch.MatchTime)
                    .OrderByDescending(m => m.MatchTime).ToList();

            List<string> matchIdSet = matchSet.Select(m => m.MatchId).ToList();
            List<TeamStat_Sina> teamStatSet = daTeamStat.Set.Where(s => matchIdSet.Contains(s.MatchId)).ToList();
            //include all the team avg stat
            List<TeamStatAvgModel> teamStatAvgModelSet = GetAllTeamStat(mainMatch.MatchTime);

            result.Match.Team_Sina = teamSet.FirstOrDefault(t => t.TeamId == mainMatch.HomeTeamId);
            result.Match.Team_Sina1 = teamSet.FirstOrDefault(t => t.TeamId == mainMatch.VisitingTeamId);

            result.HomeTeamHistory.IsAtHome = true;
            result.HomeTeamHistory.Team = result.Match.Team_Sina;
            result.VisitingTeamHistory.IsAtHome = false;
            result.VisitingTeamHistory.Team = result.Match.Team_Sina1;

            List<TeamStat_Sina> homeTeamStatSet = new List<TeamStat_Sina>();
            List<TeamStat_Sina> homeOppositeStatSet = new List<TeamStat_Sina>();
            List<TeamStat_Sina> visitingTeamStatSet = new List<TeamStat_Sina>();
            List<TeamStat_Sina> visitingOppositeStatSet = new List<TeamStat_Sina>();
            List<TeamStat_Sina> asHomeTeamStatSet = new List<TeamStat_Sina>();
            List<TeamStat_Sina> asHomeOppositeStatSet = new List<TeamStat_Sina>();
            List<TeamStat_Sina> asVisitingTeamStatSet = new List<TeamStat_Sina>();
            List<TeamStat_Sina> asVisitingOppositeStatSet = new List<TeamStat_Sina>();
            List<MatchStatModel> homeMatchStatModelSet = new List<MatchStatModel>();
            List<MatchStatModel> visitingMatchStatModelSet = new List<MatchStatModel>();

            foreach (Match_Sina match in matchSet)
            {
                match.Team_Sina = teamSet.FirstOrDefault(t => t.TeamId == match.HomeTeamId);
                match.Team_Sina1 = teamSet.FirstOrDefault(t => t.TeamId == match.VisitingTeamId);
                TeamStat_Sina homeTeamStat = teamStatSet.FirstOrDefault(s => s.MatchId == match.MatchId && s.TeamId == match.HomeTeamId);
                TeamStat_Sina visitingTeamStat = teamStatSet.FirstOrDefault(s => s.MatchId == match.MatchId && s.TeamId == match.VisitingTeamId);

                if (match.HomeTeamId == mainMatch.HomeTeamId || match.VisitingTeamId == mainMatch.HomeTeamId)
                {
                    if (match.HomeTeamId == mainMatch.HomeTeamId)
                    {
                        asHomeTeamStatSet.Add(homeTeamStat);
                        asHomeOppositeStatSet.Add(visitingTeamStat);
                        homeTeamStatSet.Add(homeTeamStat);
                        homeOppositeStatSet.Add(visitingTeamStat);
                    }
                    else
                    {
                        homeTeamStatSet.Add(visitingTeamStat);
                        homeOppositeStatSet.Add(homeTeamStat);
                    }
                    homeMatchStatModelSet.Add(new MatchStatModel()
                    {
                        Match = match,
                        HomeTeamStat = homeTeamStat,
                        HomeTeamStatAvg = teamStatAvgModelSet.FirstOrDefault(s => s.Team.TeamId == match.HomeTeamId),
                        VisitingTeamStat = visitingTeamStat,
                        VisitingTeamStatAvg = teamStatAvgModelSet.FirstOrDefault(s => s.Team.TeamId == match.VisitingTeamId)
                    });
                }

                if (match.HomeTeamId == mainMatch.VisitingTeamId || match.VisitingTeamId == mainMatch.VisitingTeamId)
                {
                    if (match.VisitingTeamId == mainMatch.VisitingTeamId)
                    {
                        asVisitingTeamStatSet.Add(visitingTeamStat);
                        asVisitingOppositeStatSet.Add(homeTeamStat);
                        visitingTeamStatSet.Add(visitingTeamStat);
                        visitingOppositeStatSet.Add(homeTeamStat);
                    }
                    else
                    {
                        visitingTeamStatSet.Add(homeTeamStat);
                        visitingOppositeStatSet.Add(visitingTeamStat);
                    }
                    visitingMatchStatModelSet.Add(new MatchStatModel()
                    {
                        Match = match,
                        HomeTeamStat = homeTeamStat,
                        HomeTeamStatAvg = teamStatAvgModelSet.FirstOrDefault(s => s.Team.TeamId == match.HomeTeamId),
                        VisitingTeamStat = visitingTeamStat,
                        VisitingTeamStatAvg = teamStatAvgModelSet.FirstOrDefault(s => s.Team.TeamId == match.VisitingTeamId)
                    });
                }
            }

            result.HomeTeamHistory.AllTeamStatSet = homeTeamStatSet;
            result.HomeTeamHistory.AllOppositeStatSet = homeOppositeStatSet;
            result.HomeTeamHistory.AsTeamStatSet = asHomeTeamStatSet;
            result.HomeTeamHistory.AsOppositeStatSet = asHomeOppositeStatSet;
            result.HomeTeamHistory.MatchStatModelSet = homeMatchStatModelSet;


            result.VisitingTeamHistory.AllTeamStatSet = visitingTeamStatSet;
            result.VisitingTeamHistory.AllOppositeStatSet = visitingOppositeStatSet;
            result.VisitingTeamHistory.AsTeamStatSet = asVisitingTeamStatSet;
            result.VisitingTeamHistory.AsOppositeStatSet = asVisitingOppositeStatSet;
            result.VisitingTeamHistory.MatchStatModelSet = visitingMatchStatModelSet;

            return result;
        }

        public List<TeamStatAvgModel> GetAllTeamStat(DateTime statDate)
        {
            List<TeamStatAvgModel> result = new List<TeamStatAvgModel>();

            int year = statDate.Year;
            if (statDate.Year < 9)
            {
                year--;
            }

            statDate = statDate.Date;
            IEnumerable<TeamStatDaily_Sina> teamStatDailySet = daTeamStatDaily.Set.Where(d => d.MatchDate < statDate).Include(s => s.TeamStat_Sina).ToList();
            
            foreach (Team_Sina team in daTeam.Set)
            {
                result.Add(new TeamStatAvgModel()
                {
                    Team = team,
                    AsHomeAvg = teamStatDailySet.Where(a => a.TeamId == team.TeamId && a.StatType == 1).ToList(),
                    AsHomeOppositeAvg = teamStatDailySet.Where(a => a.TeamId == team.TeamId && a.StatType == 2).ToList(),
                    AsVisitingAvg = teamStatDailySet.Where(a => a.TeamId == team.TeamId && a.StatType == 3).ToList(),
                    AsVisitingOppositeAvg = teamStatDailySet.Where(a => a.TeamId == team.TeamId && a.StatType == 4).ToList()
                });
            }

            return result;
        }

        public static string ReturnColor(DataWinner.Nba.Model.Portal.ResultColorModel model)
        {
            string result = "#ccff99";
            if ((model.HomeScoring > model.VisitingScoring && model.TeamId == model.HomeId) ||
                (model.HomeScoring < model.VisitingScoring && model.TeamId == model.VisitingId))
            {
                result = "#ffcccc";


                if (model.HomeSection > 0 && model.VisitingSection > 0)
                {
                    //result = model.HomeSection.ToString() + "/" + model.VisitingSection + "/" + model.TeamId + "/" + model.HomeId;
                    //match win, section lose, show green
                    if ((model.HomeSection < model.VisitingSection && model.TeamId == model.HomeId) ||
                        (model.HomeSection > model.VisitingSection && model.TeamId == model.VisitingId))
                    {
                        result = "#33ff33";
                    }
                }

            }
            else
            {
                if (model.HomeSection > 0 && model.VisitingSection > 0)
                {
                    //match lose, section win, show red
                    if ((model.HomeSection > model.VisitingSection && model.TeamId == model.HomeId) ||
                        (model.HomeSection < model.VisitingSection && model.TeamId == model.VisitingId))
                    {
                        result = "#ff6666";
                    }
                }
            }
            return result;
        }       
    }
}
